Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Mongodb - How can I combine two "query.$or" statements with "query.$and"?

I want to filter the documents that satisfy

(title = searchWord OR content = searchWord) AND (category = category OR category = category).

And I want to combine two OR statements, which are option 1 and 2 below, and I tried to do, but it doesn't work! Please help me to rectify it.

I want to keep the form of option 1 and option 2, and combine the two OR statements with AND .

    //option 1
    query.$or = [{ title: searchWord }, { content: searchWord }]; 

    //option 2
    query.$or = [{ category: category }, { category: category }];


    //It doesn't work!!
    query.$and = [{$or : [{ title: searchWord }, { content: searchWord }]},
                  {$or : [{ category: category }, { category: category }]}];
    
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is the mongodb query which can be of use to you. It uses an implicit and. Make sure to replace your collectionName.

db.collectionName.find({
    $or : [ 
        {"title": searchWord},
        {"content": searchWord}
    ],
    "category": category         
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!